//
//  YLProductDetailHeader.m
//  YLFinanceService
//
//  Created by she1323 on 2017/8/28.
//  Copyright © 2017年 she1323. All rights reserved.
//  产品详情头部

#import "YLProductDetailHeader.h"
#import <Masonry.h>
#import "YLGlobeHeader.h"
@interface YLProductDetailHeader ()


@property (weak, nonatomic)  UIView *titleBaseView;
// 年化收益率
@property (nonatomic, weak) UILabel *rateOfReturnLabel;
// 已投资
@property (nonatomic, weak) UILabel *progressLabel;
// 投资进度条
@property (nonatomic, weak) UIView *progressBar;
@property (nonatomic, weak) UIImageView *backGroundImageView;

@end

@implementation YLProductDetailHeader

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI
{
    __weak typeof(self) weakSelf = self;
    
    UIImageView *imageView = [[UIImageView alloc] init];
    _backGroundImageView = imageView;
    imageView.image = [UIImage imageNamed:@"productBackground"];
    [self addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.top.bottom.mas_equalTo(weakSelf);
    }];
    
    UILabel *rateOfReturnLabel = [[UILabel alloc] init];
    rateOfReturnLabel.text = @"0.00%";
    rateOfReturnLabel.textColor = [UIColor whiteColor];
    rateOfReturnLabel.font = YLboldFont(40);
    [self addSubview:rateOfReturnLabel];
    [rateOfReturnLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(weakSelf.mas_centerY);
        make.centerX.mas_equalTo(weakSelf);
    }];
    
    UILabel *rateOfReturnTextLabel = [[UILabel alloc] init];
    _rateOfReturnLabel = rateOfReturnLabel;
    rateOfReturnTextLabel.text = @"预期年化收益率";
    rateOfReturnTextLabel.textColor = [UIColor whiteColor];
    rateOfReturnTextLabel.font = YLFont(15);
    [self addSubview:rateOfReturnTextLabel];
    [rateOfReturnTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(rateOfReturnLabel.mas_bottom).mas_offset(YLHeight(5));
        make.centerX.mas_equalTo(weakSelf);
    }];
    
    UIView *titleBaseView = [[UIView alloc] init];
    _titleBaseView = titleBaseView;
    titleBaseView.backgroundColor = [UIColor clearColor];
    [self addSubview:titleBaseView];
    [titleBaseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(weakSelf);
        make.bottom.mas_equalTo(weakSelf).mas_offset(-YLHeight(30));
        make.height.mas_equalTo(44);
    }];
    
    for (NSInteger i = 0; i < 3; i ++) {
        UILabel *label = [[UILabel alloc] init];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        label.numberOfLines = 0;
        [_titleBaseView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.mas_equalTo(weakSelf.titleBaseView);
            make.width.mas_equalTo(YLScreenW / 3.0);
            make.left.mas_equalTo(weakSelf.titleBaseView).mas_offset(i * YLScreenW / 3.0);
        }];
    }
    
    UIView *progressBarBaseView = [[UIView alloc] init];
    progressBarBaseView.backgroundColor = YLColor(155, 34, 41);
    [self addSubview:progressBarBaseView];
    [progressBarBaseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(weakSelf);
        make.height.mas_equalTo(YLHeight(5));
    }];
    
    UIView *progressBar = [[UIView alloc] init];
    _progressBar = progressBar;
    progressBar.backgroundColor = YLColor(240, 184, 192);
    [self addSubview:progressBar];
    [progressBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.mas_equalTo(weakSelf);
        make.height.mas_equalTo(YLHeight(5));
        make.width.mas_equalTo(0);
    }];
    
    UILabel *progressLabel = [[UILabel alloc] init];
    _progressLabel = progressLabel;
    progressLabel.textColor = YLColorA(240, 240, 240, 0.7);
    progressLabel.text = @"已认购0%";
    progressLabel.font = YLFont(12.0F);
    [self addSubview:progressLabel];
    [progressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(weakSelf);
        make.bottom.mas_equalTo(progressBarBaseView.mas_top).mas_offset(-5);
    }];
}

-(void)setHeaderModel:(YLProductDetaiHeaderModel *)headerModel
{
    __weak typeof(self) weakSelf = self;
    _headerModel = headerModel;
    _progressLabel.text = [NSString stringWithFormat:@"已认购%.1f%%", headerModel.investmentPercentage];
    _rateOfReturnLabel.text = headerModel.rateOfReturn;
    
    [self layoutIfNeeded];
    
    if (headerModel.investmentPercentage == 0) {
        
    } else {
    }
    [_progressBar mas_updateConstraints:^(MASConstraintMaker *make) {
         make.width.mas_equalTo(YLScreenW * (headerModel.investmentPercentage / 100.0));
    }];
    
    if (headerModel.investmentPercentage > 10) {
        
        if (headerModel.investmentPercentage > 90) {
            [_progressLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(weakSelf);
                make.bottom.mas_equalTo(_progressBar.mas_top).mas_offset(-5);
            }];
        } else {
            [_progressLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(weakSelf.mas_left).mas_offset(YLScreenW * (headerModel.investmentPercentage / 100.0));
                make.bottom.mas_equalTo(_progressBar.mas_top).mas_offset(-5);
            }];
        }
        
        
        
    }
    [UIView animateWithDuration:1.5 animations:^{
        [self layoutIfNeeded];
    }];
    
    for (NSInteger i = 0; i < _titleBaseView.subviews.count; i ++) {
        UILabel *label = _titleBaseView.subviews[i];
        YLProductHeaderTitleModel *m = headerModel.detailHeaderTitles[i];
        label.attributedText = [self attStringWithTitle:m.title content:m.content];
    }
}


- (void)setOffsetY:(CGFloat)offsetY
{
    _offsetY = offsetY;
    __weak typeof(self) weakSelf = self;
    [_backGroundImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(weakSelf).mas_offset(offsetY);
        make.right.mas_equalTo(weakSelf).mas_equalTo(fabs(offsetY));
    }];
    
}

- (NSMutableAttributedString *)attStringWithTitle:(NSString *)title content:(NSString *)content
{
    NSMutableAttributedString *att1 = [[NSMutableAttributedString alloc] initWithString:title attributes:@{NSFontAttributeName :YLFont(13), NSForegroundColorAttributeName : YLColorA(240, 240, 240, 0.7)}];
    
    [att1 appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n\n" attributes:@{NSFontAttributeName : YLFont(4)}]];
    
    [att1 appendAttributedString:[[NSMutableAttributedString alloc] initWithString:content attributes:@{NSFontAttributeName : YLboldFont(15)}]];
    
    return att1;
}



@end
